package com.spdg.ring.util;

import java.io.File;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import cn.wolf.http.ProgressListener;
import cn.wolf.http.entity.BitmapProgress;
import cn.wolf.tools.CommonUtils;
import cn.wolf.tools.ImageFactory;
import cn.wolf.tools.SharedPreferencesUtil;

import com.spdg.ring.R;
import com.spdg.ring.common.Config;
import com.spdg.ring.common.Key;
import com.spdg.ring.datamgr.BitmapCacheMgr;

public class ImageLoader extends ImageFactory {

	private static ImageLoader mInstance = new ImageLoader();

	public static ImageLoader getInstance() {
		if (mInstance == null)
			mInstance = new ImageLoader();
		return mInstance;
	}

	@Override
	public String getOperateDir() {
		return Config.SDCARD_ARTICLE_PIC;
	}

	@Override
	public void cacheBitmap(Bitmap cache, String cacheKey) {
		BitmapCacheMgr.getInstance().saveBitmap(cacheKey, cache);
	}

	@Override
	public Bitmap getCache(String cacheKey) {
		return BitmapCacheMgr.getInstance().getBitmap(cacheKey);
	}

	@Override
	public int bitmapResourceIdDef() {
		return R.drawable.default_image;
	}

	public Bitmap loadBitmapOvri(Context context, final View view, final String url, String cacheKey) {
		mContext = context;
		return super.loadBitmap(view, url, cacheKey, bitmapResourceIdDef());
	}

	@Override
	protected Bitmap loadBitmap(final View view, final String url,
			final String cacheKey, final ProgressListener<BitmapProgress> listener,
			boolean isCompress, int junst, int resId) {

		String dir = getOperateDir();
		String path = dir + "/" + cacheKey;

		if (CommonUtils.is3g2g(mContext)) {
			boolean imageSetting = SharedPreferencesUtil.getBoolean(mContext,
					Key.K_2G3G_IMAGE_SETTING);
			if (imageSetting) {
				File file = new File(path);
				if (file.exists()) {
					file.delete();
				}
			}
		}

		return super.loadBitmap(view, url, cacheKey, listener, isCompress, junst, resId);
	}
}
